package com.dlc.shop.delivery.multishop.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.BmapPoint;
import com.dlc.shop.delivery.common.model.SameCity;
import com.dlc.shop.delivery.common.service.SameCityService;
import com.dlc.shop.security.multishop.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * @author lhd
 * @date 2020-05-23 08:26:25
 */
@RestController
@RequestMapping("/delivery/sameCity")
@Tag(name = "商家端同城配送接口")
@AllArgsConstructor
public class SameCityController {

    private final SameCityService sameCityService;

    @GetMapping("/getSameCityInfo")
    @Operation(summary = "获取同城配送数据")
    @PreAuthorize("@pms.hasPermission('delivery:sameCity:info')")
    public ServerResponseEntity<SameCity> getSameCityPage() {
        SameCity sameCity = sameCityService.getSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        if (Objects.nonNull(sameCity)) {
            JSONArray jsonArray = JSONUtil.parseArray(sameCity.getPositionIds());
            sameCity.setPolygonPath(jsonArray.toList(BmapPoint.class));
        }
        return ServerResponseEntity.success(sameCity);
    }

    @PostMapping
    @Operation(summary = "新增同城配送数据")
    @PreAuthorize("@pms.hasPermission('delivery:sameCity:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid SameCity sameCity) {
        sameCity.setShopId(SecurityUtils.getShopUser().getShopId());
        if (CollUtil.isEmpty(sameCity.getPolygonPath())) {
            throw new YamiShopBindException("请设置配送区域");
        }
        sameCityService.saveSameCity(sameCity);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改同城配送数据")
    @PreAuthorize("@pms.hasPermission('delivery:sameCity:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid SameCity sameCity) {
        JSONArray jsonArray = new JSONArray(sameCity.getPolygonPath());
        sameCity.setPositionIds(jsonArray.toString());
        sameCity.setShopId(SecurityUtils.getShopUser().getShopId());
        boolean flag = sameCityService.updateById(sameCity);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success(flag);
    }

    @DeleteMapping("/{samecityId}")
    @Operation(summary = "根据id删除同城配送数据")
    @Parameter(name = "samecityId", description = "同城配送id" , required = true)
    @PreAuthorize("@pms.hasPermission('delivery:sameCity:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long samecityId) {
        boolean flag = sameCityService.removeById(samecityId);
        sameCityService.removeSameCityByShopId(SecurityUtils.getShopUser().getShopId());
        return ServerResponseEntity.success(flag);
    }
}
